package com.hxy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.common.DateUtil;
import com.hxy.common.JsonResult;
import com.hxy.dao.AddressbookMapper;
import com.hxy.dao.BranchMapper;
import com.hxy.dao.BusinessMapper;
import com.hxy.dao.StuffinfoMapper;
import com.hxy.pojo.Branch;
import com.hxy.pojo.model.BranchDatas;
import com.hxy.pojo.vo.BranchVo;
import com.hxy.service.BranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private StuffinfoMapper stuffinfoMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private AddressbookMapper addBookMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        /**
         *  *  1.根据branchid改员工表的Branch为未分配
         *      * 2.根据branchid改职位表的Branch_Id
         *      * 3.改通讯部门id (bid)为0
         *      * 4.删除branch的对应部门
         */
        if (id != 0) {
            //1.根据branchid改员工表(stuffinfo)的Branch为未分配 0
            int i1 = stuffinfoMapper.updateBranchByBranchId(id);
            //2.根据branchid改职位表(business)的Branch_Id为0
            int i2 = businessMapper.updateBranchIdById(id);
            //3.根据branchid改通讯录id为0
            addBookMapper.updateBranchIdById(id);
            //4.删除branch的对应部门
            int i3 = branchMapper.deleteByPrimaryKey(id);
            return i1 + i2 + i3;
        } else {
            int i3 = branchMapper.deleteByPrimaryKey(id);
            return 0;
        }
    }

    @Override
    public int insert(Branch record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Branch record) {
        return 0;
    }

    @Override
    public Branch selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public PageInfo<BranchVo> selectAllPagerBranch(Integer page, int limit, String name, String phone, Integer branch) {
        PageHelper.startPage(page, limit);
        BranchDatas branchDatas = new BranchDatas(page, limit, name, phone, branch);
        List<BranchVo> Branchs = branchMapper.selectAllPagerBranch(branchDatas);

        //根据id查寻部门人数
        for (BranchVo branch1 : Branchs) {
            int num = stuffinfoMapper.selectBranchPersonNum(branch1.getId());
            branch1.setPersonNumber(num);
        }
        PageInfo<BranchVo> BranchsVoPageInfo = new PageInfo<>(Branchs);
        System.out.println(BranchsVoPageInfo);
        return BranchsVoPageInfo;
    }

    @Override
    public BranchVo selectOneBranchInfo(Integer id) {
        BranchVo branchVo = branchMapper.selectOneBranchInfo(id);
        return branchVo;
    }

    /**
     * 部门信息修改
     *
     * @param branchVo
     * @return
     */
    @Override
    public int updateOneBranchInfo(BranchVo branchVo) {
        //根据branchVo的"部长名" 和Phone 唯一查询 "员工表" 部长的id存入到部门表
        Integer BEid = stuffinfoMapper.selectEidByStuffName(branchVo.getStuffName(), branchVo.getStuffPhone());
        //如果BEid存在插入
        if (BEid != null) {
            branchVo.setBeid(BEid);
            System.out.println("修改的数据: //updateOneBranchInfo***2" + branchVo);
            int i = branchMapper.updateOneBranchInfo(branchVo);
            return i;
        }
        //不存在返回错误
        else {
            return 0;
        }

    }

    /**
     * 添加部门
     *
     * @param branchVo
     * @return
     */
    @Override
    public String addOneBranchInfo(BranchVo branchVo) {
        //branchVo 包含/部门名 branchname/部长名字 stuffName/部门电话 bphone
//        //根据部长名字和手机号获取唯一部长id
        Integer BEid = stuffinfoMapper.selectEidByStuffName(branchVo.getStuffName(), branchVo.getStuffPhone());
//        //过去当前创建时间
        String createtime = DateUtil.date2Str(new Date(), DateUtil.DATE_FORMAT_1);
//        //获取是否存在重名部门
//        int branchNameNum=branchMapper.selectBranchNameNumByName(branchVo.getBranchname());
//        //获取是否部门联系电话重名
////        int branchPhoneNum=branchMapper.selectBranchPhoneNumByName(branchVo.getBphone());
//        //判断部长id是否存在不存在返回失败
////        if (BEid!=null&&branchNameNum==0&&branchPhoneNum==0){
//            //BEid存在就添加且部门名不重复且部门电话不重复
        branchVo.setBeid(BEid);
        branchVo.setCreatetime(createtime);
        System.out.println("**service/addOneBranchInfo:" + branchVo);
        int i = branchMapper.addOneBranchInfo(branchVo);
        return "添加成功";
    }

    /**
     * @param branchVo
     * @param checkflag
     * @return
     */
    @Override
    public JsonResult checkAddForm(BranchVo branchVo, int checkflag) {
        if (checkflag == 1) {
            Integer BEid = stuffinfoMapper.selectEidByStuffName(branchVo.getStuffName(), branchVo.getStuffPhone());
            if (BEid != null) {
                return new JsonResult(0, "success", BEid);
            } else {
                return new JsonResult(1, "职员不存在", BEid);
            }
        } else if (checkflag == 2) {
            int branchNameNum = branchMapper.selectBranchNameNumByName(branchVo.getBranchname(), branchVo.getId());
            if (branchNameNum > 0) {
                if (branchVo.getId() != null) {
                    Branch branchCopy = branchMapper.selectByPrimaryKey(branchVo.getId());
                    if (branchCopy.getBranchname().equals(branchVo.getBranchname())) {
                        return new JsonResult(2, "部门没有更改", branchCopy.getBphone());
                    } else {
                        return new JsonResult(1,"部门已经存在",null);
                    }
                }
                else {
                    return new JsonResult(1, "部门已经存在", branchNameNum);
                }

            } else {
                return new JsonResult(0, "部门可以插入", branchNameNum);
            }
        } else if (checkflag == 3) {
            int branchPhoneNum = branchMapper.selectBranchPhoneNumByName(branchVo.getBphone(), branchVo.getId());

            if (branchPhoneNum > 0) {
                if (branchVo.getId() != null) {
                    Branch branchPhoneCopy = branchMapper.selectByPrimaryKey(branchVo.getId());
                    if (branchPhoneCopy.getBphone().equals(branchVo.getBphone())) {
                        return new JsonResult(2, "部门电话没有更改", branchPhoneCopy.getBphone());
                    } else {
                        return new JsonResult(1, "部门电话被占用", null);
                    }
                } else {
                    return new JsonResult(1, "部门电话已经存在", branchPhoneNum);
                }
            } else {
                return new JsonResult(0, "部门电话可以使用", branchPhoneNum);
            }
        } else {
            return new JsonResult(1, "异常", null);
        }
    }
}
